package fi.helsinki.cs.ohtu10k.view;

import javax.swing.*;

import java.awt.event.*;
import com.sun.java.layout.SpringUtilities;

import fi.helsinki.cs.ohtu10k.schedule.*;

/**
 * Class implementing dialog for deletion of courses.
 * @author Hannu Kärnä
 * @author Thomas Nyman
 *
 */
@SuppressWarnings("serial")
public class DeleteCourseDialog extends JDialog implements ActionListener{
	
	private JPanel panel;
	private JButton buttonDeleteCourse, buttonCancel;
	private JComboBox selectorCourse;

	/**
	 * Displays delete course dialog.
	 * @param owner Owning frame.
	 */
	public static void showDeleteCourseDialog(JFrame owner) {	
		if ( ScheduleManagerHandler.getScheduleManager().getCourses().length==0 ) {
			JOptionPane.showMessageDialog(owner, 
										  Constants.LABEL_ERROR_NO_COURSES,
										  Constants.LABEL_DELETE_COURSE_TITLE,
										  JOptionPane.PLAIN_MESSAGE);
		} else {		
			new DeleteCourseDialog(owner);
		}
	}

	/**
	 * Creates a new delete course dialog.
	 * @param owner Owning frame.
	 */
	public DeleteCourseDialog(JFrame owner) {
		super(owner, Constants.LABEL_DELETE_COURSE_TITLE, true);

		panel = new JPanel();
		panel.setLayout(new SpringLayout());
		
		// Initialization of interactive components
		selectorCourse = new JComboBox(ScheduleManagerHandler.getScheduleManager().getCourses());	
		buttonDeleteCourse = new JButton(Constants.LABEL_DELETE_COURSE);
		buttonCancel = new JButton(Constants.LABEL_CANCEL);
		
		// Add components to panel
		panel.add(new JLabel(Constants.LABEL_DELETE_COURSE_SELECT));
		panel.add(selectorCourse);
		panel.add(new JPanel());
		panel.add(new JPanel());
		panel.add(buttonDeleteCourse);
		panel.add(buttonCancel);
		
		buttonDeleteCourse.addActionListener(this);
		buttonCancel.addActionListener(this);
		
		// Build compact layout
		SpringUtilities.makeCompactGrid(panel,
                						3, 2,	//rows, columns
                						18, 12,	//initX, initY
                						18, 12);//xPad, yPad
		this.setContentPane(panel);
		pack();
	    setLocationRelativeTo(owner);
	    setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==buttonDeleteCourse) {
			ScheduleManagerHandler.getScheduleManager().delCourse((ScheduleCourse)selectorCourse.getSelectedItem());
			setVisible(false);
		} else setVisible(false);
	}
	
}
